//
//  FunctionTestView.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/10.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class FunctionTestView: UIView {

    var content : Array<Dictionary<String,Any>> = []
    
    override init(frame : CGRect){
        super.init(frame: frame)
        self.layer.cornerRadius = 4 * YXWidthScale
        self.layer.borderColor = UIColor.colorWithHexString(hex: "f2f2f2").cgColor
        self.layer.borderWidth = 2 * YXWidthScale
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configContent(model : PhoneInfoModel) -> Void {
        
    }
    
    func configBottom() -> Void {
        let tray = UIView.init(frame: CGRect.init(x: 0, y: 528 * YXHeightScale, width: self.width(), height: 88 * YXHeightScale))
        tray.backgroundColor = UIColor.colorWithHexString(hex: "f8f9fb")
        self.addSubview(tray)
        let font = UIFont.systemFont(ofSize: 26 * YXWidthScale)
        let size = "图例:".StringSize(font: font)
        let label1 = UILabel.init(frame: CGRect.init(x: 40 * YXWidthScale, y: tray.height() / 2 - size.height / 2, width: size.width, height: size.height))
        label1.font = font
        label1.text = "图例:"
        tray.addSubview(label1)
        
        let normal = functionView(item: "正常", state: 0, origin: CGPoint.init(x: 142 * YXWidthScale, y: label1.top()))
        tray.addSubview(normal)
        let p = CGPoint.init(x: normal.right() + 40 * YXWidthScale, y: normal.top())
        let abnormal = functionView(item: "异常", state: 1, origin: p)
        tray.addSubview(abnormal)
        let p1 = CGPoint.init(x: abnormal.right() + 40 * YXWidthScale, y: abnormal.top())
        let cha = functionView(item: "没有权限", state: 2, origin: p1)
        tray.addSubview(cha)
    }
    
    func configContent() -> Void {
        let backL = UIView.init()
        backL.backgroundColor = UIColor.colorWithHexString(hex: "f8f9fb")
        backL.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: self.width(), height: 88 * YXHeightScale))
        self.addSubview(backL)
        let titleLbl = UILabel.init()
        titleLbl.textColor = UIColor.black
        let font = UIFont.systemFont(ofSize: 32 * YXWidthScale)
        titleLbl.font = font
        titleLbl.text = "功能检测"
        let size = "功能检测".StringSize(font: font)
        titleLbl.frame = CGRect.init(x: self.width() / 2 - size.width / 2, y: backL.height() / 2 - size.height / 2, width: size.width, height: size.height)
        backL.addSubview(titleLbl)
        let back = UIView.init()
        back.backgroundColor = UIColor.white
        self.addSubview(back)
        if content.count > 0 {
            var idx = 0
            for item in content {
                let origin = CGPoint.init(x: 38 * YXWidthScale + CGFloat(idx % 3) * 212 * YXWidthScale, y: 30 * YXHeightScale + CGFloat(idx / 3) * 88 * YXHeightScale)
                let cnt = item["content"] as! String
                let status = item["status"] as! Int
                let view = functionView(item: cnt, state: status, origin: origin)
                back.addSubview(view)
                if idx == content.count - 1 {
                    back.frame = CGRect.init(x: 0, y: 88 * YXHeightScale, width: self.width(), height: view.bottom())
                }
                idx = idx + 1
            }
        }
        configBottom()
    }
    
    func functionView(item : String,state : Int,origin : CGPoint)->UIView{
        let font = UIFont.systemFont(ofSize: 26 * YXWidthScale)
        let size = item.StringSize(font: font)
        let view = UIView.init()
        let imageView = UIImageView.init(frame: CGRect.init(x: 2 * YXWidthScale, y: 2 * YXHeightScale, width: 26 * YXWidthScale, height: 26 * YXHeightScale))
        if state == 0 {
            imageView.image = UIImage.load(name: "icon_right")
        }else if state == 1 {
            imageView.image = UIImage.load(name: "icon_error")
        }else {
            imageView.image = UIImage.load(name: "icon_abnormal")
        }
        view.addSubview(imageView)
        
        let label = UILabel.init(frame: CGRect.init(x: imageView.right() + 20 * YXWidthScale, y: 2 * YXHeightScale, width: size.width, height: size.height))
        label.textColor = UIColor.colorWithHexString(hex: "646464")
        label.text = item
        label.font = font
        label.numberOfLines = 3
        view.addSubview(label)
        view.frame = CGRect.init(x: origin.x, y: origin.y, width: imageView.width() + label.width() + 20 * YXWidthScale, height: label.height())
        return view
    }

}
